<template>
  <div class="app">
    <div class="venue_wrap df_sb">
      <div class="df_ac">
        <strong class="f_size18 mr6">{{ activeVenue.name }}</strong>
        <!-- <el-popover ref="venueListRef" placement="bottom-end" trigger="click">
          <div class="venueList">
            <div v-for="item in venueList" :key="item.id" class="item" @click="venueClick(item)">{{ item.name }}</div>
          </div>
          <img slot="reference" class="pointer" style="width: 22px" src="@/assets/icon/zhankai.png" alt="">
        </el-popover> -->
      </div>
      <el-button
        v-if="meta.indexOf('SaveHomeRenovationInfo') > -1"
        v-preventReClick
        :loading="subLoading"
        type="primary"
        @click="onSubmit"
      >{{ translate("saveAndPub") }}</el-button>
    </div>
    <div class="mini_main df">
      <div class="module_wrap">
        <div class="df_sb">
          <strong class="f_size18">{{ translate("homeModule") }}</strong>
          <el-popover
            ref="popoverRef"
            popper-class="myself_popper"
            placement="bottom-start"
            trigger="click"
          >
            <div class="module_list_able">
              <div
                v-for="(item, index) in moduleList.filter(
                  (i) => i.isDeleted == '1'
                )"
                :key="index"
                class="item"
                @click="popoverItemclcik(item.code)"
              >
                {{ item.name }}
              </div>
            </div>
            <template slot="reference">
              <el-button
                type="primary"
                icon="el-icon-plus"
                :disabled="
                  moduleList.filter((i) => i.isDeleted == '1').length === 0
                "
              >{{ translate("moduleAdd") }}</el-button>
            </template>
          </el-popover>
        </div>
        <div class="f_size12 mt10 mb10 placeholder9">
          {{ translate("ableDrop") }}
        </div>
        <div class="module_list">
          <Container @drop="onDrop">
            <Draggable
              v-for="item in moduleList.filter((i) => i.isDeleted == '0')"
              :key="item.name"
            >
              <div
                class="item"
                :class="{ active: activeItem.code === item.code }"
                @click="moduleItemClick(item)"
              >
                <img class="drag" src="@/assets/icon/drag.png" alt="">
                <template v-if="!item.isEdit">
                  <div>
                    <span class="mr6">{{ item.name }}</span>
                    <i
                      v-if="item.iscanRename == '1'"
                      class="el-icon-edit pointer"
                      @click="moduleEditClick(item.code, true)"
                    />
                  </div>
                  <div class="fade df">
                    <img
                      v-if="
                        item.content.isDisplay === '1' &&
                          item.iscanDisplay == '1'
                      "
                      style="width: 22px"
                      class="pointer iteM_SHOP_icon mr10"
                      src="@/assets/icon/zhuangxiu_ic_xianshi.png"
                      alt=""
                      @click="isShowClick(item.code, '0')"
                    >
                    <img
                      v-if="
                        item.content.isDisplay === '0' &&
                          item.iscanDisplay == '1'
                      "
                      style="width: 22px"
                      class="pointer mr10"
                      src="@/assets/icon/zhuangxiu_ic_yincang_hover.png"
                      alt=""
                      @click="isShowClick(item.code, '1')"
                    >
                    <img
                      v-if="item.content.isDisplay === '0'"
                      style="width: 22px; opacity: 0"
                      class="iteM_SHOP_icon_none"
                      src="@/assets/icon/zhuangxiu_ic_yincang_hover.png"
                    >
                    <img
                      v-if="item.required != '1'"
                      style="width: 22px"
                      class="iteM_SHOP_icon pointer"
                      src="@/assets/icon/zhuangxiu_ic_delete.png"
                      alt=""
                      @click="moduleItemDel(item.code)"
                    >
                  </div>
                </template>

                <template v-if="item.isEdit">
                  <div class="input_wrap flex1 mr10">
                    <el-input
                      v-model="item.content.name"
                      :maxlength="item.maxNameNum"
                    />
                    <span
                      class="text"
                    >{{
                      (item.content.name && item.content.name.length) || 0
                    }}/{{ item.maxNameNum }}</span>
                  </div>
                  <span
                    v-preventReClick
                    class="pointer primaryColor mr10"
                    @click="moduleNameSave(item.code, false, item.content.name)"
                  >{{ translate("save") }}</span>
                  <img
                    src="@/assets/icon/zhuangxiu_ic_cancel.png"
                    class="pointer"
                    alt=""
                    @click="moduleEditClick(item.code, false)"
                  >
                </template>
              </div>
            </Draggable>
          </Container>
        </div>
      </div>
      <div class="occupied mr20" />
      <div class="simulator_wrapper">
        <div class="simulator_wrap">
          <div class="simulator">
            <img class="StatusBar" src="@/icons/png/StatusBar.png" alt="">
            <template v-if="moduleList.length > 0">
              <telShow
                :config="moduleList"
                :glabo-config="miniConfig"
                :active-item="activeItem"
                :venue-info="activeVenue"
              />
            </template>
          </div>
        </div>
      </div>
      <div v-if="moduleList.length > 0" class="option_wrap df">
        <div class="occupied mr20 ml20" />
        <!-- 轮播 -->
        <Slideshow
          v-show="activeItem.code === 'M_SHOP_BANNER'"
          ref="SlideshowRef"
          :config="bannerConfig"
        />
        <!-- 功能入口 -->
        <Modules
          v-show="activeItem.code === 'M_SHOP_FUNCTION'"
          ref="ModulesRef"
          :config="modulesConfig"
        />
        <!-- 热门套餐 -->
        <RecomCombo
          v-show="activeItem.code === 'M_SHOP_MEALS'"
          ref="RecomComboRef"
          :config="RecomComboConfig"
        />
        <!-- 热门课程 -->
        <HotCourse
          v-show="activeItem.code === 'M_SHOP_COURSE'"
          ref="HotCourseRef"
          :config="hotCourseConfig"
        />
        <!-- 近期活动 -->
        <Activity
          v-show="activeItem.code === 'M_SHOP_ACTIVITY'"
          ref="ActivityRef"
          :config="activityConfig"
        />
        <!-- 体育运动 -->
        <Sports
          v-show="activeItem.code === 'M_SHOP_KNOWLEDGE'"
          ref="SportsRef"
          :config="sportsConfig"
        />
        <!-- 教练风采 -->
        <Coaching
          v-show="activeItem.code === 'M_SHOP_TEACHER'"
          ref="CoachingRef"
          :config="coachConfig"
        />
        <!-- 秒杀活动 -->
        <Seckill
          v-show="activeItem.code === 'M_SHOP_SECKILL'"
          ref="SeckillRef"
          :config="seckillConfig"
        />
        <Discount
          v-show="activeItem.code === 'M_SHOP_SECDISCOUNT'"
          ref="DiscountRef"
          :config="discountConfig"
        />
        <!-- 热门场馆 -->
        <HotVen
          v-show="activeItem.code === 'M_SHOP_VENUE'"
          ref="VenhowRef"
          :config="venConfig"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { Container, Draggable } from 'vue-smooth-dnd'
import { applyDrag } from '../../util/drag'
import Slideshow from './components-option/slideshow.vue'
import HotVen from './components-option/hotVen.vue'
import Modules from './components-option/modules.vue'
import RecomCombo from './components-option/recomCombo.vue'
import Activity from './components-option/activity.vue'
import HotCourse from './components-option/hotCourse.vue'
import Sports from './components-option/sports.vue'
import telShow from './show.vue'
import Coaching from './components-option/Coaching.vue'
import Seckill from './components-option/Seckill.vue'
import Discount from './components-option/Discount.vue'

import {
  getHomeRenovationInfo,
  updateHomeRenovationInfo
} from '@/api'
export default {
  components: {
    HotVen,
    Container,
    Draggable,
    telShow,
    Slideshow,
    Modules,
    RecomCombo,
    Activity,
    HotCourse,
    Sports,
    Coaching,
    Seckill,
    Discount
  },
  props: {
    config: {
      type: Object,
      default: () => ({})
    },
    meta: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      moduleList: [],
      moduleListTemp: [],
      activeItem: {},
      activeVenue: {},
      subLoading: false,
      venConfig: {},
      bannerConfig: {},
      modulesConfig: {},
      venueConfig: {},
      RecomComboConfig: {},
      activityConfig: {},
      hotCourseConfig: {},
      sportsConfig: {},
      coachConfig: {},
      seckillConfig: {},
      discountConfig: {},

      miniConfig: {}
    }
  },
  watch: {
    config: {
      handler(newValue) {
        this.miniConfig = newValue
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.initHomeData()
  },
  methods: {
    onSubmit() {
      const { moduleList } = this
      moduleList.forEach(item => {
        if (item.code === 'M_SHOP_BANNER') { // banner
          item.content = this.$refs.SlideshowRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.SlideshowRef.showContent.join(',')
          this.$refs.SlideshowRef.indexSub()
          item.venueContent = this.$refs.SlideshowRef.venueContent
        }
        if (item.code === 'M_SHOP_FUNCTION') { // 功能入口
          item.content = this.$refs.ModulesRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.venueContent = this.$refs.ModulesRef.venueContent
        }
        if (item.code === 'M_SHOP_MEALS') { // 套餐
          item.content = this.$refs.RecomComboRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.RecomComboRef.showContent.join(',')
          this.$refs.RecomComboRef.indexSub()
          item.venueContent = this.$refs.RecomComboRef.venueContent
        }
        if (item.code === 'M_SHOP_VENUE') { // 热门场馆
          item.content = this.$refs.VenhowRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.VenhowRef.showContent.join(',')
          this.$refs.VenhowRef.indexSub()
          item.venueContent = this.$refs.VenhowRef.venueContent
        }
        if (item.code === 'M_SHOP_COURSE') { // 热门课程
          item.content = this.$refs.HotCourseRef.content
          item.content.param = this.$refs.HotCourseRef.showContent.join(',')
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          this.$refs.HotCourseRef.indexSub()
          item.venueContent = this.$refs.HotCourseRef.venueContent
        }
        if (item.code === 'M_SHOP_ACTIVITY') { // 近期活动
          item.content = this.$refs.ActivityRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.ActivityRef.showContent.join(',')
          this.$refs.ActivityRef.indexSub()
          item.venueContent = this.$refs.ActivityRef.venueContent
        }
        if (item.code === 'M_SHOP_KNOWLEDGE') { // 体育运动
          item.content = this.$refs.SportsRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.SportsRef.showContent.join(',')
          this.$refs.SportsRef.indexSub()
          item.venueContent = this.$refs.SportsRef.venueContent
        }
        if (item.code === 'M_SHOP_TEACHER') { // 教练风采
          item.content = this.$refs.CoachingRef.content
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          item.content.param = this.$refs.CoachingRef.showContent.join(',')
          this.$refs.CoachingRef.indexSub()
          item.venueContent = this.$refs.CoachingRef.venueContent
        }
        if (item.code === 'M_SHOP_SECKILL') { // 秒杀活动
          item.content = this.$refs.SeckillRef.content
          // item.content.param = this.$refs.SeckillRef.showContent.join(',')
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          this.$refs.SeckillRef.indexSub()
          item.venueContent = this.$refs.SeckillRef.venueContent
        }
        if (item.code === 'M_SHOP_SECDISCOUNT') { // 限时折扣
          item.content = this.$refs.DiscountRef.content
          // item.content.param = this.$refs.DiscountRef.showContent.join(',')
          item.content.name = item.name
          item.content.isDisplay = item.isDisplay
          item.content.isDeleted = item.isDeleted
          this.$refs.DiscountRef.indexSub()
          item.venueContent = this.$refs.DiscountRef.venueContent
        }
      })
      const param = {
        code: 'T_SHOP_HOME',
        homeInfos: moduleList.filter(i => i.isDeleted === '0')
      }
      this.subLoading = true
      updateHomeRenovationInfo({ param }).then(res => {
        this.subLoading = false
        if (res.errorCode !== '000000') return
        this.$store.dispatch('app/resetMiniFlag')
        this.$message.success(this.translate('saveScs'))
      }, () => {
        this.subLoading = false
      })
    },
    initHomeData() {
      const { activeVenue } = this
      getHomeRenovationInfo({ param: { code: 'T_SHOP_HOME', venueId: activeVenue.id }}).then(res => {
        if (res.errorCode !== '000000') return
        this.moduleList = [...res.recordList]
        this.moduleListTemp = this.moduleList.map(item => {
          const obj = {
            name: item.name,
            isDisplay: item.isDisplay,
            isDeleted: item.isDeleted
          }
          return obj
        })

        if (this.moduleList.filter(i => i.isDeleted === '0').length > 0) {
          this.activeItem = { ...this.moduleList.filter(i => i.isDeleted === '0')[0] }
        }

        this.moduleList.forEach(item => {
          switch (item.code) {
            case 'M_SHOP_BANNER':
              this.bannerConfig = { venueId: activeVenue.id, ...item }
              break
            case 'M_SHOP_VENUE':
              this.venConfig = { venueId: activeVenue.id, ...item }
              break
            case 'M_SHOP_FUNCTION':
              this.modulesConfig = item
              break
            case 'M_SHOP_MEALS':
              this.RecomComboConfig = { venueId: activeVenue.id, venueName: activeVenue.name, ...item }
              break
            case 'M_SHOP_COURSE':
              this.hotCourseConfig = { venueId: activeVenue.id, venueName: activeVenue.name, ...item }
              break
            case 'M_SHOP_ACTIVITY':
              this.activityConfig = { venueId: activeVenue.id, venueName: activeVenue.name, ...item }
              break
            case 'M_SHOP_KNOWLEDGE':
              this.sportsConfig = { venueId: activeVenue.id, venueName: activeVenue.name, ...item }
              break
            case 'M_SHOP_TEACHER':
              this.coachConfig = { venueId: activeVenue.id, ...item }
              break
            case 'M_SHOP_SECKILL':
              this.seckillConfig = { venueId: activeVenue.id, ...item }
              break
            case 'M_SHOP_SECDISCOUNT':
              this.discountConfig = { venueId: activeVenue.id, ...item }
              break
            default:
              break
          }
          item.content.name = item.name
        })
      })
    },
    compareModule() {
      const { moduleList, moduleListTemp } = this
      let count = 0
      moduleListTemp.forEach((item, i) => {
        if (item.name !== moduleList[i].name) {
          count++
        }
        if (item.isDeleted !== moduleList[i].isDeleted) {
          count++
        }
        if (item.isDisplay !== moduleList[i].isDisplay) {
          count++
        }
      })
      this.$store.dispatch('app/changeMiniFlag', {
        code: 'homeModuleFlag',
        flag: count > 0
      })
    },
    moduleItemClick(val) {
      this.activeItem = val
      this.$bus.$emit('simulatorScoll', val)
    },
    isShowClick(code, flag) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].content.isDisplay = flag
      moduleList[index].isDisplay = flag
      this.moduleList = moduleList
      this.compareModule()
    },
    popoverItemclcik(val) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === val)
      moduleList[index].isDeleted = '0'
      this.moduleList = moduleList
      this.$refs.popoverRef.doClose()
      this.compareModule()
    },
    moduleItemDel(code) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].isDeleted = '1'
      this.moduleList = moduleList
      this.compareModule()
      this.$set(this.activeItem, 'code', '')
    },
    moduleEditClick(code, falg) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].isEdit = falg
      this.moduleList = moduleList
    },
    moduleNameSave(code, falg, name) {
      const moduleList = [...this.moduleList]
      const index = moduleList.findIndex(i => i.code === code)
      moduleList[index].isEdit = falg
      moduleList[index].content.name = name
      moduleList[index].name = name
      this.moduleList = moduleList
      this.compareModule()
    },
    onDrop(dropResult) {
      this.moduleList = applyDrag(this.moduleList, dropResult)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.venueList {
  .item {
    padding: 10px;
    cursor: pointer;
    &:hover {
      background-color: #f5f7fc;
    }
  }
}
.module_wrap {
  width: 300px;
  height: 100%;
  padding: 20px;
  background-color: #fff;
  font-size: 14px;
  flex: none;

  .module_list {
    height: calc(100% - 20px);
    overflow-y: auto;

    &::-webkit-scrollbar {
      display: none;
    }

    width: 260px;

    .item {
      position: relative;
      width: 260px;
      height: 60px;
      padding: 0 20px;
      background: #ffffff;
      border: 1px solid #dfe2e8;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;
      cursor: pointer;

      .iteM_SHOP_icon {
        display: none;

        img {
          cursor: pointer;
        }
      }

      &:hover {
        .iteM_SHOP_icon {
          display: block;
        }

        .iteM_SHOP_icon_none {
          display: none;
        }
      }

      .input_wrap {
        position: relative;

        .text {
          position: absolute;
          top: 11px;
          right: 10px;
          font-size: 12px;
          color: #999999;
        }

        ::v-deep .el-input__inner {
          padding-right: 46px;
        }
      }

      .drag {
        position: absolute;
        width: 16px;
        height: 16px;
        left: 0;
        top: 0;
      }
    }

    .active {
      background: #f4f7fc;
      border: 1px solid #435ebe;
    }
  }
}
.simulator_wrap {
  width: 818px;
  height: 1464px;
  padding: 32px 34px;
  flex: none;
  background-color: #fff;
  position: relative;
  background: url("~@/icons/png/ic_shouji.png");
  background-size: 100% 100%;
  /* Old IE only */
  -moz-transform: scale(0.34);
  -webkit-transform: scale(0.34);
  transform: scale(0.34);
  transform-origin: top left;
  .simulator {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 50px;
    overflow: hidden;
    background-color: #f7f7f7;
    .StatusBar {
      height: 88px;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1000;
    }
  }
}
.occupied {
  width: 4px;
  height: 100%;
  background-color: #e4e7ed;
}
.option_wrap {
  background-color: #fff;
  flex: 1;
  margin-left: -540px;
}

.module_list_able {
  width: 203px;
  min-height: 100px;
  border-radius: 2px;
  padding: 5px;
  display: flex;
  flex-wrap: wrap;
  .item {
    width: 86px;
    height: 32px;
    background: #ffffff;
    border-radius: 2px;
    border: 1px solid #dfe2e8;
    /* border: 1px solid #435EBE; */
    /* color: $primaryColor; */
    margin: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
}
.mini_main {
  /* background-color: #f5f6fd; */
  height: calc(100% - 140px);
}

.app {
  height: calc(100%);
}
</style>
